package com.athui.utils.explorer.metadata.impl;

import com.athui.bean.metadata.ColumnMetaData;
import com.athui.bean.config.DataType;
import com.athui.bean.metadata.TableMetaData;
import com.athui.utils.common.ConfigureUtils;
import com.athui.utils.common.jdbc.JdbcResultSetBuilder;
import com.athui.utils.explorer.metadata.JdbcExplorer;
import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.common.typeinfo.TypeInformation;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: TODO
 * @autor: zhangzhonghui
 * @create: 2024-08-01 13:19
 * @Version: 1.0
 */
public class OracleJdbcExplorer extends JdbcExplorer {


    public Map<String, DataType> typeMapping(){
        Map<String, DataType> typeMapping = new HashMap<>();
        // 字符型
        typeMapping.put("VARCHAR2", new DataType(BasicTypeInfo.STRING_TYPE_INFO,DataType.TEXT));
        // 数值型
        typeMapping.put("NUMBER", new DataType(BasicTypeInfo.BIG_DEC_TYPE_INFO,DataType.NUMERICAL));
        typeMapping.put("bigint", new DataType(BasicTypeInfo.LONG_TYPE_INFO,DataType.NUMERICAL));

        // 日期型
        typeMapping.put("DATE", new DataType(BasicTypeInfo.DATE_TYPE_INFO,DataType.DATE));
        //typeMapping.put("Timestamp", LocalTimeTypeInfo.LOCAL_DATE_TIME);

        // 字节
        typeMapping.put("RAW",new DataType(TypeInformation.of(byte[].class),DataType.BYTE));


        return typeMapping;
    }

    @Override
    public ColumnMetaData columnMetaDataBuilder(ColumnMetaData columnMetaData) {
        return columnMetaData;
    }

    @Override
    public TableMetaData tableMetaDataBuilder(String schemaName,String tableName) {
        return operation(new JdbcResultSetBuilder<TableMetaData>(){

            @Override
            public String buildSql() {
                Map<String, Object> params =new HashMap<>();
                params.put("schema_name",schemaName);
                params.put("table_name",tableName);
                return ConfigureUtils.mapperParser("oracleTableMetadataSql",params).toString();
            }

            @Override
            public TableMetaData accept(ResultSet rs) throws SQLException {
                TableMetaData tableMetaData = new TableMetaData();
                while (rs.next()){
                    tableMetaData.setSchemaName(rs.getString("schema_name"));
                    tableMetaData.setTableName(rs.getString("table_name"));
                    tableMetaData.setRowSize(rs.getInt("row_size"));
                    tableMetaData.setTableType(rs.getString("table_type"));
                    tableMetaData.setTableComment(rs.getString("table_comment"));
                    tableMetaData.setDataSizeKB(rs.getLong("data_size_kb"));
                    tableMetaData.setCreateTime(rs.getString("create_time"));
                    tableMetaData.setLastUpdateTime(rs.getString("last_update_time"));
                    tableMetaData.setPartitioned("YES".equals(rs.getString("partitioned")));
                }
                return tableMetaData;
            }
        });
    }
}
